﻿using Kang.DbDoc.Interfaces;
using Kang.DbDoc.Models;
using Kang.ExtendMethod;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.SS.Formula.Functions;

namespace Kang.DbDoc.ServicesImpl
{
    public class ExcelExportService : IExport
    {
        public bool Export(string filename, DatabaseModel database, List<TableOrViewModel> tables, List<TableOrViewModel> views, List<RoutineModel> functions, List<RoutineModel> procedures, ref string error)
        {
            ISheet tableInfoSheet = null;
            ISheet viewInfoSheet;
            ISheet functionInfoSheet;
            ISheet procedureInfoSheet;
            try
            {
                IWorkbook workbook = new XSSFWorkbook();
                ISheet databaseInfoSheet = workbook.CreateSheet("数据库信息");
                ISheet fixLogSheet = workbook.CreateSheet("修订日志");
                ICellStyle titleCellStyle = workbook.CreateCellStyle();
                IFont titleCellFont = workbook.CreateFont();
                titleCellFont.FontName = "宋体";
                titleCellFont.IsBold = true;
                titleCellFont.FontHeight = 16 * 20;
                titleCellStyle.SetFont(titleCellFont);
                titleCellStyle.Alignment = HorizontalAlignment.Center;
                titleCellStyle.VerticalAlignment = VerticalAlignment.Center;

                ICellStyle subtitleCellStyle = workbook.CreateCellStyle();
                IFont subtitleCellFont = workbook.CreateFont();
                subtitleCellFont.FontName = "宋体";
                subtitleCellFont.IsBold = true;
                subtitleCellFont.FontHeight = 11 * 20;
                subtitleCellStyle.SetFont(subtitleCellFont);
                subtitleCellStyle.Alignment = HorizontalAlignment.Left;
                subtitleCellStyle.VerticalAlignment = VerticalAlignment.Center;
                subtitleCellStyle.BorderLeft = BorderStyle.Thin;
                subtitleCellStyle.BorderTop = BorderStyle.Thin;
                subtitleCellStyle.BorderRight = BorderStyle.Thin;
                subtitleCellStyle.BorderBottom = BorderStyle.Thin;

                ICellStyle normalCellStyle = workbook.CreateCellStyle();
                IFont normalCellFont = workbook.CreateFont();
                normalCellFont.FontName = "宋体";
                normalCellFont.IsBold = false;
                normalCellFont.FontHeight = 11 * 20;
                normalCellStyle.WrapText = true;
                normalCellStyle.SetFont(normalCellFont);
                normalCellStyle.Alignment = HorizontalAlignment.Left;
                normalCellStyle.VerticalAlignment = VerticalAlignment.Center;
                normalCellStyle.BorderLeft = BorderStyle.Thin;
                normalCellStyle.BorderTop = BorderStyle.Thin;
                normalCellStyle.BorderRight = BorderStyle.Thin;
                normalCellStyle.BorderBottom = BorderStyle.Thin;

                ICellStyle splitterCellStyle = workbook.CreateCellStyle();
                splitterCellStyle.SetFont(normalCellFont);
                //((XSSFColor)splitterCellStyle.FillBackgroundColorColor)?.SetRgb(new byte[]{78, 170, 195});

                IRow row;
                ICell cell;
                int rowIndex = 0;

                // 地址信息
                row = databaseInfoSheet.CreateRow(rowIndex++);
                cell = CreateCell(row, 0, subtitleCellStyle);
                cell.SetCellValue("数据库");
                cell = CreateCell(row, 1, normalCellStyle);
                if (database.DatabaseType == Enums.DatabaseTypeEnum.SQLite)
                {
                    cell.SetCellValue(Path.GetFileName(database.DatabaseName));
                }
                else
                {
                    cell.SetCellValue(database.DatabaseName);
                }

                // 地址信息
                row = databaseInfoSheet.CreateRow(rowIndex++);
                cell = CreateCell(row, 0, subtitleCellStyle);
                cell.SetCellValue("地址");
                cell = CreateCell(row, 1, normalCellStyle);
                if (database.DatabaseType == Enums.DatabaseTypeEnum.SQLite)
                {
                    cell.SetCellValue(database.DatabaseName);
                }
                else
                {
                    cell.SetCellValue(database.DatabaseHost);
                }

                // 端口信息
                if (database.DatabasePort > 0)
                {
                    row = databaseInfoSheet.CreateRow(rowIndex++);
                    cell = CreateCell(row, 0, subtitleCellStyle);
                    cell.SetCellValue("端口");
                    cell = CreateCell(row, 1, normalCellStyle);
                    cell.SetCellValue(database.DatabasePort);
                }

                if (tables.IsNotBlank())
                {
                    row = databaseInfoSheet.CreateRow(rowIndex++);
                    cell = CreateCell(row, 0, subtitleCellStyle);
                    cell.SetCellValue("表总数");
                    cell = CreateCell(row, 1, normalCellStyle);
                    cell.SetCellValue(tables.Count);
                }
                if (views.IsNotBlank())
                {
                    row = databaseInfoSheet.CreateRow(rowIndex++);
                    cell = CreateCell(row, 0, subtitleCellStyle);
                    cell.SetCellValue("视图总数");
                    cell = CreateCell(row, 1, normalCellStyle);
                    cell.SetCellValue(views.Count);
                }
                if (functions.IsNotBlank())
                {
                    row = databaseInfoSheet.CreateRow(rowIndex++);
                    cell = CreateCell(row, 0, subtitleCellStyle);
                    cell.SetCellValue("方法总数");
                    cell = CreateCell(row, 1, normalCellStyle);
                    cell.SetCellValue(functions.Count);
                }
                if (procedures.IsNotBlank())
                {
                    row = databaseInfoSheet.CreateRow(rowIndex++);
                    cell = CreateCell(row, 0, subtitleCellStyle);
                    cell.SetCellValue("存储过程总数");
                    cell = CreateCell(row, 1, normalCellStyle);
                    cell.SetCellValue(procedures.Count);
                }
                databaseInfoSheet.SetColumnWidth(0, 6 * 2 * 256 + 256);
                databaseInfoSheet.SetColumnWidth(1, GetMaxCharacterCount(new List<string>()
                {
                    database.DatabaseName,
                    database.DatabaseHost,
                }, 4 * 2, 4 * 2) * 256 + 256);

                row = fixLogSheet.CreateRow(0);
                cell = CreateCell(row, 0, subtitleCellStyle);
                cell.SetCellValue("版本号");
                cell = CreateCell(row, 1, subtitleCellStyle);
                cell.SetCellValue("修订日期");
                cell = CreateCell(row, 2, subtitleCellStyle);
                cell.SetCellValue("修订内容");
                cell = CreateCell(row, 3, subtitleCellStyle);
                cell.SetCellValue("修订人");
                cell = CreateCell(row, 4, subtitleCellStyle);
                cell.SetCellValue("审核人");
                fixLogSheet.SetColumnWidth(0, 3 * 2 * 256 + 256);
                fixLogSheet.SetColumnWidth(1, 6 * 2 * 256 + 256);
                fixLogSheet.SetColumnWidth(2, 50 * 2 * 256 + 256);
                fixLogSheet.SetColumnWidth(3, 3 * 2 * 256 + 256);
                fixLogSheet.SetColumnWidth(4, 3 * 2 * 256 + 256);


                CreateBlankRow(fixLogSheet, 1, 5, normalCellStyle);
                CreateBlankRow(fixLogSheet, 2, 5, normalCellStyle);
                CreateBlankRow(fixLogSheet, 3, 5, normalCellStyle);
                CreateBlankRow(fixLogSheet, 4, 5, normalCellStyle);
                CreateBlankRow(fixLogSheet, 5, 5, normalCellStyle);

                tables.IsNotBlank(() =>
                {
                    tableInfoSheet = workbook.CreateSheet("数据库表结构");
                    tableInfoSheet.SetColumnWidth(0, 2 * 2 * 256 + 256);
                    tableInfoSheet.SetColumnWidth(1, tables.Select(x => x.Columns.Select(y => GetCharacterCount(y.Name, 2 * 2, 2 * 2)).Max()).Max() * 256 + 256);
                    tableInfoSheet.SetColumnWidth(2, tables.Select(x => x.Columns.Select(y => GetCharacterCount(y.Type, 4 * 2, 4 * 2)).Max()).Max() * 256 + 256);
                    tableInfoSheet.SetColumnWidth(3, 6 * 256 + 256);
                    tableInfoSheet.SetColumnWidth(4, 4 * 256 + 256);
                    tableInfoSheet.SetColumnWidth(5, tables.Select(x => x.Columns.Select(y => GetCharacterCount(y.KeyType, 3 * 2, 3 * 2)).Max()).Max() * 256 + 256);
                    tableInfoSheet.SetColumnWidth(6, 4 * 256 + 256);
                    tableInfoSheet.SetColumnWidth(7, 4 * 256 + 256);
                    tableInfoSheet.SetColumnWidth(8, tables.Select(x => x.Columns.Select(y => GetCharacterCount(y.Default, 2 * 2, 2 * 2)).Max()).Max() * 256 + 256);
                    tableInfoSheet.SetColumnWidth(9, tables.Select(x => x.Columns.Select(y => GetCharacterCount(y.Comment, 3 * 2, 3 * 2)).Max()).Max() * 256 + 256);
                    rowIndex = 0;
                    for (int tableIndex = 0, id = 0; tableIndex < tables.Count; tableIndex++, id = 0)
                    {
                        var table = tables[tableIndex];
                        row = CreateBlankRow(tableInfoSheet, rowIndex++, 10, titleCellStyle);
                        cell = CreateCell(row, 0, titleCellStyle);
                        cell.SetCellValue(table.Name);
                        tableInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 9));
                        if (table.Comment.IsNotBlank())
                        {
                            row = CreateBlankRow(tableInfoSheet, rowIndex++, 10, splitterCellStyle);
                            cell = CreateCell(row, 0, splitterCellStyle);
                            cell.SetCellValue(table.Comment);
                            tableInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 9));
                        }

                        row = tableInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("序号");

                        cell = CreateCell(row, 1, subtitleCellStyle);
                        cell.SetCellValue("列名");

                        cell = CreateCell(row, 2, subtitleCellStyle);
                        cell.SetCellValue("数据类型");

                        cell = CreateCell(row, 3, subtitleCellStyle);
                        cell.SetCellValue("长度");

                        cell = CreateCell(row, 4, subtitleCellStyle);
                        cell.SetCellValue("小数");

                        cell = CreateCell(row, 5, subtitleCellStyle);
                        cell.SetCellValue("键类型");

                        cell = CreateCell(row, 6, subtitleCellStyle);
                        cell.SetCellValue("自增");

                        cell = CreateCell(row, 7, subtitleCellStyle);
                        cell.SetCellValue("为空");

                        cell = CreateCell(row, 8, subtitleCellStyle);
                        cell.SetCellValue("默认");

                        cell = CreateCell(row, 9, subtitleCellStyle);
                        cell.SetCellValue("列说明");

                        foreach (var col in table.Columns)
                        {
                            row = tableInfoSheet.CreateRow(rowIndex++);
                            cell = CreateCell(row, 0, normalCellStyle);
                            cell.SetCellValue($"{++id}");

                            cell = CreateCell(row, 1, normalCellStyle);
                            cell.SetCellValue(col.Name);

                            cell = CreateCell(row, 2, normalCellStyle);
                            cell.SetCellValue($"{col.Type}");

                            cell = CreateCell(row, 3, normalCellStyle);
                            cell.SetCellValue($"{col.Length}");

                            cell = CreateCell(row, 4, normalCellStyle);
                            cell.SetCellValue($"{col.Scale}");

                            cell = CreateCell(row, 5, normalCellStyle);
                            cell.SetCellValue($"{col.KeyType}");

                            cell = CreateCell(row, 6, normalCellStyle);
                            cell.SetCellValue($"{(col.IsAutoIncrement ? "√" : "")}");

                            cell = CreateCell(row, 7, normalCellStyle);
                            cell.SetCellValue($"{(col.IsNull ? "√" : "")}");

                            cell = CreateCell(row, 8, normalCellStyle);
                            cell.SetCellValue($"{col.Default}");

                            cell = CreateCell(row, 9, normalCellStyle);
                            cell.SetCellValue($"{col.Comment}");
                        }
                        if (tableIndex + 1 < tables.Count)
                        {
                            // 空白行，用于分割
                            row = CreateBlankRow(tableInfoSheet, rowIndex++, 10, splitterCellStyle);
                            cell = CreateCell(row, 0, splitterCellStyle);
                            tableInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 9));
                        }
                    }
                });

                views.IsNotBlank(() =>
                {
                    viewInfoSheet = workbook.CreateSheet("数据库视图结构");
                    viewInfoSheet.SetColumnWidth(0, 2 * 2 * 256 + 256);
                    viewInfoSheet.SetColumnWidth(1, views.Select(x => x.Columns.Select(y => GetCharacterCount(y.Name, 2 * 2, 2 * 2)).Max()).Max() * 256 + 256);
                    viewInfoSheet.SetColumnWidth(2, views.Select(x => x.Columns.Select(y => GetCharacterCount(y.Type, 4 * 2, 4 * 2)).Max()).Max() * 256 + 256);
                    viewInfoSheet.SetColumnWidth(3, 6 * 256 + 256);
                    viewInfoSheet.SetColumnWidth(4, 4 * 256 + 256);
                    viewInfoSheet.SetColumnWidth(5, views.Select(x => x.Columns.Select(y => GetCharacterCount(y.KeyType, 3 * 2, 3 * 2)).Max()).Max() * 256 + 256);
                    viewInfoSheet.SetColumnWidth(6, 4 * 256 + 256);
                    viewInfoSheet.SetColumnWidth(7, 4 * 256 + 256);
                    viewInfoSheet.SetColumnWidth(8, views.Select(x => x.Columns.Select(y => GetCharacterCount(y.Default, 2 * 2, 2 * 2)).Max()).Max() * 256 + 256);
                    viewInfoSheet.SetColumnWidth(9, views.Select(x => x.Columns.Select(y => GetCharacterCount(y.Comment, 3 * 2, 3 * 2)).Max()).Max() * 256 + 256);
                    rowIndex = 0;
                    for (int viewIndex = 0, id = 0; viewIndex < views.Count; viewIndex++, id = 0)
                    {
                        var view = views[viewIndex];
                        row = CreateBlankRow(viewInfoSheet, rowIndex++, 10, titleCellStyle);
                        cell = CreateCell(row, 0, titleCellStyle);
                        cell.SetCellValue(view.Name);
                        viewInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 9));
                        if (view.Comment.IsNotBlank())
                        {
                            row = CreateBlankRow(viewInfoSheet, rowIndex++, 10, splitterCellStyle);
                            cell = CreateCell(row, 0, splitterCellStyle);
                            cell.SetCellValue(view.Comment);
                            viewInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 9));
                        }

                        row = viewInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("序号");

                        cell = CreateCell(row, 1, subtitleCellStyle);
                        cell.SetCellValue("列名");

                        cell = CreateCell(row, 2, subtitleCellStyle);
                        cell.SetCellValue("数据类型");

                        cell = CreateCell(row, 3, subtitleCellStyle);
                        cell.SetCellValue("长度");

                        cell = CreateCell(row, 4, subtitleCellStyle);
                        cell.SetCellValue("小数");

                        cell = CreateCell(row, 5, subtitleCellStyle);
                        cell.SetCellValue("键类型");

                        cell = CreateCell(row, 6, subtitleCellStyle);
                        cell.SetCellValue("自增");

                        cell = CreateCell(row, 7, subtitleCellStyle);
                        cell.SetCellValue("为空");

                        cell = CreateCell(row, 8, subtitleCellStyle);
                        cell.SetCellValue("默认");

                        cell = CreateCell(row, 9, subtitleCellStyle);
                        cell.SetCellValue("列说明");

                        foreach (var col in view.Columns)
                        {
                            row = viewInfoSheet.CreateRow(rowIndex++);
                            cell = CreateCell(row, 0, normalCellStyle);
                            cell.SetCellValue($"{++id}");

                            cell = CreateCell(row, 1, normalCellStyle);
                            cell.SetCellValue(col.Name);

                            cell = CreateCell(row, 2, normalCellStyle);
                            cell.SetCellValue($"{col.Type}");

                            cell = CreateCell(row, 3, normalCellStyle);
                            cell.SetCellValue($"{col.Length}");

                            cell = CreateCell(row, 4, normalCellStyle);
                            cell.SetCellValue($"{col.Scale}");

                            cell = CreateCell(row, 5, normalCellStyle);
                            cell.SetCellValue($"{col.KeyType}");

                            cell = CreateCell(row, 6, normalCellStyle);
                            cell.SetCellValue($"{(col.IsAutoIncrement ? "√" : "")}");

                            cell = CreateCell(row, 7, normalCellStyle);
                            cell.SetCellValue($"{(col.IsNull ? "√" : "")}");

                            cell = CreateCell(row, 8, normalCellStyle);
                            cell.SetCellValue($"{col.Default}");

                            cell = CreateCell(row, 9, normalCellStyle);
                            cell.SetCellValue($"{col.Comment}");
                        }
                        if (viewIndex + 1 < views.Count)
                        {
                            // 空白行，用于分割
                            row = CreateBlankRow(viewInfoSheet, rowIndex++, 10, splitterCellStyle);
                            cell = CreateCell(row, 0, splitterCellStyle);
                            viewInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 9));
                        }
                    }
                });
                
                // 方法数据处理
                functions.IsNotBlank(() =>
                {
                    functionInfoSheet = workbook.CreateSheet("数据库方法结构");
                    functionInfoSheet.SetColumnWidth(0, 3 * 2 * 256 + 256);
                    functionInfoSheet.SetColumnWidth(1, functions.Select(x => GetMaxCharacterCount(x.Definition.Split(new char[] { '\n' }).ToList())).Max() * 256 + 256);
                    rowIndex = 0;
                    for (int functionIndex = 0, id = 0; functionIndex < functions.Count; functionIndex++, id = 0)
                    {
                        var function = functions[functionIndex];
                        row = functionInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("方法");
                        cell = CreateCell(row, 1, normalCellStyle);
                        cell.SetCellValue(function.Name);

                        row = functionInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("注解");
                        cell = CreateCell(row, 1, normalCellStyle);
                        cell.SetCellValue(function.Comment);

                        row = functionInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("创建者");
                        cell = CreateCell(row, 1, normalCellStyle);
                        cell.SetCellValue(function.Definer);

                        row = CreateBlankRow(functionInfoSheet, rowIndex++, 2, normalCellStyle);
                        row.Height = (short)(functionInfoSheet.DefaultRowHeight * (function.Definition.ToCharArray().Where(x => x == '\n').Count() + 1));
                        cell = CreateCell(row, 0, normalCellStyle);
                        cell.SetCellValue(function.Definition);
                        functionInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 1));
                        if (functionIndex + 1 < functions.Count)
                        {
                            // 空白行，用于分割
                            row = CreateBlankRow(functionInfoSheet, rowIndex++, 2, splitterCellStyle);
                            cell = CreateCell(row, 0, splitterCellStyle);
                            functionInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 1));
                        }
                    }
                });

                // 存储过程数据处理
                procedures.IsNotBlank(() =>
                {
                    procedureInfoSheet = workbook.CreateSheet("数据库存储过程结构");
                    procedureInfoSheet.SetColumnWidth(0, 4 * 2 * 256 + 256);
                    procedureInfoSheet.SetColumnWidth(1, procedures.Select(x => GetMaxCharacterCount(x.Definition.Split(new char[] { '\n' }).ToList())).Max() * 256 + 256);
                    rowIndex = 0;
                    for (int procedureIndex = 0, id = 0; procedureIndex < procedures.Count; procedureIndex++, id = 0)
                    {
                        var procedure = procedures[procedureIndex];
                        row = procedureInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("存储过程");
                        cell = CreateCell(row, 1, normalCellStyle);
                        cell.SetCellValue(procedure.Name);

                        row = procedureInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("注解");
                        cell = CreateCell(row, 1, normalCellStyle);
                        cell.SetCellValue(procedure.Comment);

                        row = procedureInfoSheet.CreateRow(rowIndex++);
                        cell = CreateCell(row, 0, subtitleCellStyle);
                        cell.SetCellValue("创建者");
                        cell = CreateCell(row, 1, normalCellStyle);
                        cell.SetCellValue(procedure.Definer);

                        row = CreateBlankRow(procedureInfoSheet, rowIndex++, 2, normalCellStyle);
                        row.Height = (short)(procedureInfoSheet.DefaultRowHeight * (procedure.Definition.ToCharArray().Where(x => x == '\n').Count() + 1));
                        cell = CreateCell(row, 0, normalCellStyle);
                        cell.SetCellValue(procedure.Definition);
                        procedureInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 1));
                        if (procedureIndex + 1 < procedures.Count)
                        {
                            // 空白行，用于分割
                            row = CreateBlankRow(procedureInfoSheet, rowIndex++, 2, splitterCellStyle);
                            cell = CreateCell(row, 0, splitterCellStyle);
                            procedureInfoSheet.AddMergedRegion(new CellRangeAddress(row.RowNum, row.RowNum, 0, 1));
                        }
                    }
                });
                
                using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    workbook.Write(fs);
                }
                return true;
            }
            catch (Exception e)
            {
                error = e.Message;
                return false;
            }
        }

        private ICell CreateCell(IRow row, int colIndex, ICellStyle style = null)
        {
            var cell = row.CreateCell(colIndex);
            if (style != null)
            {
                cell.CellStyle = style;
            }
            return cell;
        }

        private IRow CreateBlankRow(ISheet sheet, int rowIndex, int cellCount = 0, ICellStyle style = null)
        {
            var row = sheet.CreateRow(rowIndex);
            if (cellCount > 0)
            {
                for (int i = 0; i < cellCount; i++)
                {
                    var cell = row.CreateCell(i);
                    if (style != null)
                    {
                        cell.CellStyle = style;
                    }
                }
            }
            return row;
        }

        private int GetCharacterCount(string str, int default_w = 0, int min_w = 0)
        {
            if (str == null)
            {
                return default_w;
            }
            if (str.IndexOfAny(new char[] { '\r', '\n' }) != -1)
            {
                var strSpit = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                return GetMaxCharacterCount(strSpit, default_w, min_w);
            }
            var len = Encoding.Default.GetByteCount(str);
            len = len <= 254 ? len : 254;
            len = len < min_w ? min_w : len;
            return len;
        }

        /// <summary>
        /// 计算字符串字符长度，汉子为2两个字符长度，可设置长度
        /// </summary>
        /// <param name="strList"></param>
        /// <param name="default_w">默认值</param>
        /// <param name="min_w">最低值</param>
        /// <returns></returns>
        private int GetMaxCharacterCount(List<string> strList, int default_w = 0, int min_w = 0)
        {
            if (strList == null)
            {
                return default_w;
            }
            var max = strList.Select(x => GetCharacterCount(x)).Max();
            max = max < min_w ? min_w : max;
            return max <= 0 ? default_w : max;
        }
    }
}
